package com.xps.purifierapp.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jude.utils.JUtils;
import com.xps.purifierapp.R;
import com.xps.purifierapp.bean.AZBean;
import com.xps.purifierapp.date.net.UserNetWorks;
import com.xps.purifierapp.utils.SharedPreferencesUtils;
import com.xps.purifierapp.view.wigdet.LoadingDialog;

import rx.Subscriber;

/**
 * Created by liwenhan on 2017/11/6.
 */


public class IntroductionFragment extends Fragment implements View.OnClickListener {


    ImageView actionbarIvBack;
    TextView actionbarTvTitle;
    Button btnOk;
    EditText newpassword, oldpassword, okpassword;
    LoadingDialog loadingDialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_introduction, null);
        actionbarIvBack = view.findViewById(R.id.actionbar_iv_back);
        actionbarTvTitle = view.findViewById(R.id.actionbar_tv_title);
        btnOk = view.findViewById(R.id.btn_okupdate);
        newpassword = view.findViewById(R.id.Edit_newPassword);
        oldpassword = view.findViewById(R.id.Edit_oldpassword);
        okpassword = view.findViewById(R.id.Edit_OKPassword);
        actionbarIvBack.setVisibility(View.GONE);
        actionbarTvTitle.setText("修改密码");
        btnOk.setOnClickListener(this);
        loadingDialog=new LoadingDialog(getActivity());
        return view;
    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_okupdate:
                if (!okpassword.getText().toString().equals("") && okpassword.getText().toString() != null) {
                    if (newpassword.getText().toString().equals(okpassword.getText().toString())) {
                        getUpdatePassword();
                        loadingDialog.setMessage("正在提交").show();
                    } else {
                        JUtils.Toast("两次密码不一样");
                    }
                } else {
                    JUtils.Toast("请输入新密码");
                }
                break;
        }


    }


    void getUpdatePassword() {
        UserNetWorks.getUpdatepass(newpassword.getText().toString(), oldpassword.getText().toString(), new Subscriber<AZBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                loadingDialog.dismiss();
            }

            @Override
            public void onNext(AZBean azBean) {
                loadingDialog.dismiss();
                if (azBean.getReturn_code().equals("SUCCESS")) {
                    SharedPreferencesUtils.putString("userPassword", "String", newpassword.getText().toString());
                    Toast.makeText(getActivity(), azBean.getReturn_msg(), Toast.LENGTH_LONG).show();
                } else if (azBean.getReturn_code().equals("FALL")) {
                    Toast.makeText(getActivity(), azBean.getReturn_msg(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
